package com.atguigu.guli.service.edu.service.impl;

import com.atguigu.guli.service.edu.entity.Teacher;
import com.atguigu.guli.service.edu.entity.query.TeacherSearch;
import com.atguigu.guli.service.edu.mapper.TeacherMapper;
import com.atguigu.guli.service.edu.service.TeacherService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2021-07-27
 */
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {

    @Override
    public Page<Teacher> selectPageByCondition(Integer pageNum, Integer pageSize, TeacherSearch teacherSearch) {
        Page<Teacher> page = new Page<>(pageNum , pageSize);

        LambdaQueryWrapper<Teacher> queryWrapper = new LambdaQueryWrapper<>();

        //判断传入的条件是否存在，如果存在 则设置到queryWrapper中
        String name = teacherSearch.getName();
        Integer level = teacherSearch.getLevel();
        String joinDateBegin = teacherSearch.getJoinDateBegin();
        String joinDateEnd = teacherSearch.getJoinDateEnd();
        if(!StringUtils.isEmpty(name)){
            queryWrapper.likeRight(Teacher::getName ,name);// name%   mysql 索引 如果模糊查询  以%开始 会导致索引失效
        }
        if(level!=null){
            queryWrapper.eq(Teacher::getLevel , level);
        }
        if(!StringUtils.isEmpty(joinDateBegin)){
            queryWrapper.ge(Teacher::getJoinDate , joinDateBegin );// 2021-01-01  2019-12-01
        }
        if(!StringUtils.isEmpty(joinDateEnd)){
            queryWrapper.le(Teacher::getJoinDate , joinDateEnd );
        }
        baseMapper.selectPage(page , queryWrapper);
        return page;
    }
}
